﻿using System;
using System.Collections.Generic;
using Netkk.Common;
using Netkk.Common.Interfaces.Repositories;
using Netkk.Common.Interfaces.Services;
using Netkk.Common.Models;
using System.Linq;

namespace Netkk.Library.Services
{
    public class AppointmentService : IAppointmentService
    {

        private readonly IAppointmentRepository repository;

        /// <summary>
        /// Initializes a new instance of the <see cref="AppointmentService"/> class.
        /// </summary>
        /// <param name="repository">The repository.</param>
        public AppointmentService(IAppointmentRepository repository)
        {
            this.repository = repository;
        }

        //protected bool ValidateAppointment(Appointment appointment)
        //{
        //    if (String.IsNullOrEmpty(appointment.Title))
        //    {
        //        validationDictionary.AddError("Title", "Bitte einen Titel eingeben.");
        //    }
        //    if (String.IsNullOrEmpty(appointment.ShortTitle))
        //    {
        //        validationDictionary.AddError("ShortTitle", "Bitte eine Kurztitel eingeben.");
        //    }
        //    if (appointment.Date == DateTime.MinValue)
        //    {
        //        validationDictionary.AddError("MinValue", "Bitte eine Datum eingeben.");
        //    }
        //    if (appointment.Date < DateTime.Now)
        //    {
        //        validationDictionary.AddError("MinValue", "Das Datum muss in der Zukunft liegen.");
        //    }
        //    if (String.IsNullOrEmpty(appointment.Location))
        //    {
        //        validationDictionary.AddError("Location", "Bitte einen Ort eingeben.");
        //    }
        //    if (String.IsNullOrEmpty(appointment.Teaser))
        //    {
        //        validationDictionary.AddError("Teaser", "Bitte eine Kurzbeschreibung eingeben.");
        //    }
        //    if (String.IsNullOrEmpty(appointment.Description))
        //    {
        //        validationDictionary.AddError("Description", "Bitte eineBeschriebung eingeben.");
        //    }
        //    return validationDictionary.IsValid;
        //}

        /// <summary>
        /// Gets the latest appointment.
        /// </summary>
        /// <returns></returns>
        public Appointment GetLatestAppointment()
        {
            IEnumerable<Appointment> appointments = repository.Select();
            Appointment appointment = (from a in appointments
                                       where a.Date >= DateTime.Now
                                       orderby a.Date ascending
                                       select a).FirstOrDefault();
            return appointment;
        }

        /// <summary>
        /// Gets the appointment by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Appointment GetAppointmentById(Guid id)
        {
            IEnumerable<Appointment> appointments = repository.Select();
            Appointment appointment = (from a in appointments
                                       where a.Id == id
                                       select a).FirstOrDefault();
            return appointment;
        }

        public Appointment GetAppointmentByTitle(string title)
        {
            IEnumerable<Appointment> appointments = repository.Select();
            Appointment appointment = (from a in appointments
                                       where EqualsWithCleanUrl(a.Title, title)
                                       select a).FirstOrDefault();

            return appointment;
        }

        private static bool EqualsWithCleanUrl(string dirty, string input)
        {
            dirty = Tools.GetCleanUrl(dirty);
            return dirty.Equals(input, StringComparison.OrdinalIgnoreCase);
        }

        public IEnumerable<Appointment> GetNextAppointments()
        {
            IEnumerable<Appointment> allAppointments = repository.Select();
            IEnumerable<Appointment> appointments = from a in allAppointments
                                                    where a.Date >= DateTime.Now
                                                    orderby a.Date ascending
                                                    select a;
            return appointments;
        }

        public IEnumerable<Appointment> GetPastAppointments()
        {
            IEnumerable<Appointment> allAppointments = repository.Select();
            IEnumerable<Appointment> appointments = from a in allAppointments
                                                    where a.Date < DateTime.Now
                                                    orderby a.Date descending
                                                    select a;
            return appointments;
        }

        public void DeleteAppointment(Appointment appointment)
        {
            repository.Delete(appointment);
        }

        public bool SaveAppointment(Appointment appointment)
        {
            // Database logic
            try
            {
                repository.Save(appointment);
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}